﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Infrastructures;
using Models;
using ConcreteService;

namespace SponsorshipWeb.personal
{
    public partial class CreateAssoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = AuthUtil.GetAuthentication();
                if (user == null)
                    Response.Redirect("../login.html");

                #region 验证了没有
                //if (user.StudentID == 0)
                //    Response.Redirect("guide.html");
                #endregion

                if (DisplayUtil.HasException(this, Request.QueryString["ex"]))
                    return;

                #region 一个人只能创建一个社团
                //IAssociationService service = ServiceFactory.CreateInstance<IAssociationService>();
                //var obj = service.GetAssociationByCreateNO(user.UserID);
                //if (obj != null)
                //{
                //    ClientScript.RegisterStartupScript(typeof(Page), "CreateAssoc", "Alert('一个人只能创建一个社团');", true);
                //    btnNext.Visible = false;
                //    return;
                //}

                #endregion

                hfUserID.Value = user.UserID.ToString();
                string mode = Request.QueryString["mode"];

                if (mode == "edit")
                {
                    var id = Request.QueryString["id"];
                    if (id.IsNullOrEmpty())
                        Response.Redirect("view.html");

                    hfAssocID.Value = id;
                    hfMode.Value = mode;
                    BindUI(id);
                }
            }
        }

        private void BindUI(string id)
        {
            //IAssociationService service = ServiceFactory.CreateInstance<IAssociationService>();
            //var assoc = service.Get<Association>(id.ToString());

            //txtName.Value = assoc.Name;
            //ddlNature.SelectedValue = assoc.TypeNO.ToString();
            //ddlType.SelectedValue = assoc.TypeDetailNO.ToString();
            //ddlLevel.SelectedValue = assoc.NatureNO.ToString();
            //ddlAmout.SelectedValue = assoc.ASizeNo.ToString();
            //txtNumber.Value = assoc.DeptAmount.ToString();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuLogo.HasFile == false || String.IsNullOrEmpty(fuLogo.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //IAssociationService service = ServiceFactory.CreateInstance<IAssociationService>();
            //Association assoc = null;

            //if (hfMode.Value == "edit")
            //    assoc = service.Get<Association>(hfAssocID.Value);
            //else
            //    assoc = new Association();

            //IStudentService stuService = ServiceFactory.CreateInstance<IStudentService>();
            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var student = stuService.Get<Student>(hfUserID.Value);

            //if (hfMode.Value == "edit")
            //{
            //    assoc.Name = txtName.Value;
            //    assoc.Type = ddlNature.SelectedItem.Text;
            //    assoc.TypeNO = ddlNature.SelectedItem.Value.ToInt();
            //    assoc.TypeDetailName = ddlType.SelectedItem.Text;
            //    assoc.TypeDetailNO = ddlType.SelectedValue.ToInt();
            //    assoc.NatureNO = ddlLevel.SelectedItem.Value.ToInt();
            //    assoc.Nature = ddlLevel.SelectedItem.Text;
            //    assoc.ASizeNo = ddlAmout.SelectedValue.ToInt();
            //    assoc.ASizeDiscription = ddlAmout.SelectedItem.Text;
            //    assoc.DeptAmount = txtNumber.Value.ToInt();
            //    assoc.Status = CommonStatus.Pending;
            //    assoc.UpdateTime = DateTime.Now;
            //    service.Update(assoc, hfUserID.Value);
            //}
            //else
            //{
            //    assoc.Name = txtName.Value;
            //    assoc.Type = ddlNature.SelectedItem.Text;
            //    assoc.TypeNO = ddlNature.SelectedItem.Value.ToInt();
            //    assoc.TypeDetailName = ddlType.SelectedItem.Text;
            //    assoc.TypeDetailNO = ddlType.SelectedValue.ToInt();
            //    assoc.NatureNO = ddlLevel.SelectedItem.Value.ToInt();
            //    assoc.Nature = ddlLevel.SelectedItem.Text;
            //    assoc.ASizeNo = ddlAmout.SelectedValue.ToInt();
            //    assoc.ASizeDiscription = ddlAmout.SelectedItem.Text;
            //    assoc.DeptAmount = txtNumber.Value.ToInt();
            //    assoc.SchoolNO = student.SchoolNO;
            //    assoc.SchoolName = student.SchoolName;
            //    assoc.Status = CommonStatus.Pending;
            //    assoc.CreateTime = DateTime.Now;
            //    assoc.CreateUserID = student.StudentNO;
            //    assoc.CreateUserName = student.Name;

            //    var school = AppConfig.GetSchool(student.SchoolNO);
            //    if (school != null)
            //    {
            //        assoc.CityCode = school.CityNO;
            //        assoc.CityName = school.CityName;
            //        assoc.ProvinceCode = school.ProvinceNO;
            //        assoc.ProvinceName = school.ProvinceName;
            //    }

            //    assoc = service.Create(assoc, hfUserID.Value);
            //    Random ran = new Random();
            //    int RandKey = ran.Next(1000, 9999);
            //    assoc.SelfPassword = assoc.AssociationNO.ToString() + RandKey;
            //    service.Update(assoc, student.SchoolNO.ToString());

            //    Relationship ship = new Relationship();
            //    ship.RoleFrom = assoc.AssociationNO;
            //    ship.RoleTo = student.StudentNO;
            //    ship.NameFrom = assoc.Name;
            //    ship.NameTo = student.Name;
            //    ship.Status = CommonStatus.Active;
            //    ship.Fun = RelationshipFun.AssociationToStudent;
            //    utilService.CreateRelationship(ship, student.StudentNO.ToString());

            //    Relationship shipSchool = new Relationship();
            //    shipSchool.RoleFrom = assoc.SchoolNO;
            //    shipSchool.RoleTo = assoc.AssociationNO;
            //    shipSchool.NameFrom = assoc.SchoolName;
            //    shipSchool.NameTo = assoc.Name;
            //    shipSchool.Status = CommonStatus.Active;
            //    shipSchool.Fun = RelationshipFun.SchoolToAssociation;
            //    utilService.CreateRelationship(shipSchool, student.StudentNO.ToString());
            //}

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && fuLogo.HasFile)
            //{
            //    lst = utilService.ListFileSystem(hfAssocID.Value.ToInt());
            //}

            //SaveFile(fuLogo, FileType.AssoLogo, lst, assoc.AssociationNO);

            //Response.Redirect("view.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst, int assocNo)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Association\\" + assocNo + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Association;
            //fs.Belong = assocNo;
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Association/" + assocNo + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = hfUserID.Value.ToInt();

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }

        protected void ddlNature_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlType.Items.Clear();

            if (ddlNature.SelectedIndex == 0)
                return;

            var lst = AssocType.ListAssocType(ddlNature.SelectedValue);

            foreach (var item in lst)
                ddlType.Items.Add(new ListItem(item.Value, item.Key));
        }
    }
}